<html> 
<head> 
<title>Naseem's Calculator!</title>
</head>

<body>
<h2>Naseem's Calculator!</h2> 
<?php 
    // Just doing some PHP printing for fun! :) 
    echo "(Version 2.0 on April 2nd by Naseem Makiya) <br />";
    echo "Type an expression in the following box (e.g., 20.5+5*3/25). <br />" 
?>

<form name="input" action="calculator.php" method="get">
<input type="text" name="expression" />
<input type="submit" value="Calculate"/>
</form>

<ul>
	<li>Only numbers and +,-,* and / operators are allowed in the expression.</li>
	<li>The evaluation follows the standard operator precendence.</li>
	<li>The calculator does not support parentheses.</li>
	<li>The calculator handles invalid input "gracefully". It does not output PHP error messages.</li>
</ul>
<p>Here are some reasonable test cases: </p>
<ol>
	<li>A basic arithmetic operation: 3+4*5=23</li>
	<li>An expression with floating point or negative sign: -3.2+2*4-1/3 = 4.46666666666667</li>
	<li>Some typos inside operation (e.g. alphabetic letter): Invalid input expression 2d4+2</li>
</ol>

<?php
    $expression = $_GET['expression'];
    if( strlen($expression) > 0 ){
         echo "<h3>Result</h3>";
        if(preg_match("/^[\d\+\-\*\/\.]*$/", $expression)){
            if(preg_match("/(\/0){1}/", $expression)){
                echo "Undefined (I think you divided by zero!) <br />";
            }
            elseif(preg_match("/^[\*\/]/", $expression)){
                echo "Please begin the expression properly <br />";
            }
            elseif(preg_match("/[\*\-\+\/]$/", $expression)){
                echo "Please end the expression properly <br />";
            }
            elseif(preg_match("/^[\.][\+\-\*\/]/", $expression)){
                echo "Invalid expression: decimal formatting error. <br />";
            }
            elseif(preg_match("/[\.][\d]*[\.]/", $expression)){
                echo "Invalid expression: decimal formatting error. <br />";
            }
            elseif(preg_match("/([\+\-\*\/][\+\*\/]){1,}/", $expression)){
                echo "Invalid expression: Two or more operators in a row. <br />";
            }
            elseif(preg_match("/([\.][\.]){1,}/", $expression)){
                echo "Invalid expression: Two decimals in a row. <br />";
            }
            elseif(preg_match("/([\-][\-]){1,}/", $expression)){
                echo "Invalid expression: No double negatives please. <br />";
            }
            elseif(preg_match("/(([\+\-\*\/])([\.]{1,})([\+\-\*\/])){1,}/", $expression)){
                echo "Invalid expression: No tricks please. <br />";
            }
            else{ 
                eval("\$ans = $expression ;");
            }
            if ( $ans || $ans == 0 ) {
                echo "$expression = $ans"; 
            }
            else {
                echo "Please enter an input in the format of a correct mathematical operation.";
            }
        }    
        else{
            echo "Invalid input expression: $expression <br /> Digits, operators, and decimal points only please. <br /> No spaces either!";
        }
    }
?>

</body>
</html>

